package com.dayuanit.dy8.mall.springbootmall.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.OutputStream;

@RestController
public class BookImagController {

    @Value("${image.filepath}")
    private String imageFilePath;

    @RequestMapping("/image/{fileName}")
    public void showPic(@PathVariable("fileName") String fileName, HttpServletResponse response) {

        try(OutputStream os = response.getOutputStream();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(imageFilePath + fileName))) {
            byte[] buf = new byte[1024];
            int length = -1;
            while (-1 != (length = bis.read(buf))) {
                os.write(buf, 0, length);
                os.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
